<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AddNameFieldSubscriber
 *
 * @author bien.tran
 */
namespace Blog\BlogBundle\Form\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormEvents;
class AddNameFieldSubscriber implements EventSubscriberInterface{
    //put your code here
    private $factory;
    public function __construct(FormFactoryInterface $factory) {
        $this->factory = $factory;
    }
    public static function getSubscribedEvents() {
        return array(FormEvents::PRE_SET_DATA => 'preSetData');
    }
    public function preSetData(DataEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();
        if($data === null)
            return;
        if(!$data->getId())
        {
            $form->add($this->factory->createNamed('text', 'title', null, array('label' => 'Title of article', 'required' => false)));
        }
        else
        {
            $form->add($this->factory->createNamed('text', 'title', null, array('label' => 'Title of article', 'required' => false, 'read_only' => true)));
        }
    }
}

?>
